package com.my.code.testcode.并发包;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 描述:
 * 任务执行完成获取结果
 *
 * @author 54485
 * @create 2018-09-18 13:34
 */
public class CallableFuture {

    public static void main(String[] args) {
        ExecutorService executor = Executors.newCachedThreadPool();
        List<Future<String>> futures = new ArrayList<>();
        for(int i = 0;i<5;i++){

            futures.add(executor.submit(new MyTask("flag"+i)));
        }
        executor.shutdown();

        for (Future<String> future :futures) {
            try {
                System.out.println(future.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

    }
}
